package com.bymboy.emos.wx.controller;

import com.bymboy.emos.wx.common.util.R;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.iai.v20200303.IaiClient;
import com.tencentcloudapi.iai.v20200303.models.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Package: com.bymboy.emos.wx.controller
 * @ClassName: TestController
 * @Author: My
 * @CreateTime: 2021/9/25 8:35
 * @Description:
 */
@RequestMapping("/test")
@RestController
@Api("测试web接口")
@Slf4j
public class TestController {
    @Value("${tx.SecretId}")
    private String secretId;

    @Value("${tx.txSecretKey}")
    private String secretKey;

    @PostMapping("/addUser")
    @ApiOperation("添加用户")
    //@RequiresPermissions(value = {"ROOT" , "USER:ADD"}, logical = Logical.OR)
    public R add() {
        return R.ok("用户添加成功");
    }

    @PostMapping("/addPersonnel")
    @ApiOperation("增加人员库")
    public R addPersonnel(@RequestBody Personnel personnel) {
        CreateGroupResponse resp = null;
        try {
            log.info("SecretId:" + secretId);
            log.info("SecretKey:" + secretKey);

            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(secretId, secretKey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("iai.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            IaiClient client = new IaiClient(cred, "ap-shanghai", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            CreateGroupRequest req = new CreateGroupRequest();
            req.setGroupName(personnel.getGroupName());
            req.setGroupId(personnel.getGroupId());
            // 返回的resp是一个CreateGroupResponse的实例，与请求对象对应
            resp = client.CreateGroup(req);
            // 输出json格式的字符串回包
            System.out.println(CreateGroupResponse.toJsonString(resp));
            return R.ok("执行成功").put("resp", resp);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return R.error("执行失败");
    }

    @PostMapping("/delPersonnel")
    @ApiOperation("删除人员库")
    public R delPersonnel(@RequestBody Personnel personnel) {
        log.info("personnel" + personnel);
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(secretId, secretKey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("iai.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            IaiClient client = new IaiClient(cred, "ap-shanghai", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DeleteGroupRequest req = new DeleteGroupRequest();
            req.setGroupId(personnel.getGroupId());
            // 返回的resp是一个DeleteGroupResponse的实例，与请求对象对应
            DeleteGroupResponse resp = client.DeleteGroup(req);
            // 输出json格式的字符串回包
            System.out.println(DeleteGroupResponse.toJsonString(resp));
            return R.ok("执行成功").put("resp", resp);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            return R.error("执行失败");
        }
    }

    @PostMapping("/verifyFace")
    @ApiOperation("人脸验证")
    public R verifyFace(@RequestBody Face face) {
        log.info("face:" + face);
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(secretId, secretKey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("iai.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            IaiClient client = new IaiClient(cred, "ap-shanghai", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            VerifyFaceRequest req = new VerifyFaceRequest();
            req.setUrl(face.getPhoto());
            req.setPersonId(face.getUserId() + "");
            req.setQualityControl(4L);
            // 返回的resp是一个VerifyFaceResponse的实例，与请求对象对应
            VerifyFaceResponse resp = client.VerifyFace(req);
            // 输出json格式的字符串回包
            System.out.println(VerifyFaceResponse.toJsonString(resp));

            return R.ok("执行成功").put("resp", resp);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            return R.error("执行失败");
        }
    }

}

@Data
class Personnel {
    private String groupName;
    private String groupId;
}

@Data
class Face {
    private Integer userId;
    private String photo;
}